package com.ehl.capture.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.List;
import org.apache.log4j.Logger;

public class DBProxy {
    public static Logger logger = Logger.getLogger(DBProxy.class);

    public DBProxy() {
    }

    public static void printSql(String sql) {
        logger.info("执行sql语句：" + sql);
    }

    public static boolean execute(String sql) {
        printSql(sql);
        Connection con = null;

        try {
            con = DBPool.getConnection();
            if (con == null) {
                logger.error("【execute】获取数据库连接：");
                throw new RuntimeException("数据库连接失败");
            }

            Statement stat = con.createStatement();
            stat.execute(sql);
            con.close();
            return true;
        } catch (Exception var11) {
            logger.error("【execute】执行语句失败:" + sql, var11);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var10) {
                    ;
                }
            }

        }

        return false;
    }

    public static boolean execute(String dateSource, String sql) {
        printSql(sql);
        Connection con = null;

        try {
            con = DBPool.getConnection(dateSource);
            if (con == null) {
                throw new RuntimeException("数据库连接失败");
            }

            Statement stat = con.createStatement();
            stat.execute(sql);
            con.close();
        } catch (Exception var12) {
            logger.error("【getSingleResult】执行语句失败：" + sql, var12);
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var11) {
                    ;
                }
            }

        }

        return true;
    }

    public static boolean execute(String dateSource, String sql, List params) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            con = DBPool.getConnection(dateSource);
            if (con == null) {
                throw new RuntimeException("数据库连接失败");
            }

            ps = con.prepareStatement(sql);

            for(int i = 0; i < params.size(); ++i) {
                Object value = params.get(i);
                if (value != null && value instanceof Timestamp) {
                    ps.setTimestamp(i + 1, (Timestamp)value);
                } else {
                    ps.setObject(i + 1, value);
                }
            }

            ps.execute();
            return true;
        } catch (Exception var15) {
            logger.error("【getSingleResult】执行语句失败：" + sql, var15);
        } finally {
            try {
                ps.close();
                con.close();
            } catch (SQLException var14) {
                var14.printStackTrace();
            }

        }

        return false;
    }

    public static Object getSingleResult(String sql) {
        printSql(sql);
        Connection con = null;
        Object result = null;
        ResultSet rs = null;
        Statement stat = null;

        try {
            con = DBPool.getConnection();
            if (con == null) {
                throw new RuntimeException("数据库连接失败");
            }

            stat = con.createStatement();
            rs = stat.executeQuery(sql);
            if (rs != null && rs.next()) {
                result = rs.getObject(1);
            }

            return result;
        } catch (Exception var22) {
            logger.error("【getSingleResult】执行语句失败：" + sql, var22);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException var21) {
                    ;
                }
            }

            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException var20) {
                    ;
                }
            }

            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var19) {
                    var19.printStackTrace();
                }
            }

        }

        return null;
    }

    public static Object getSingleResult(String dateSource, String sql) {
        printSql(sql);
        Connection con = null;
        Object result = null;

        try {
            con = DBPool.getConnection(dateSource);
            if (con == null) {
                throw new RuntimeException("数据库连接失败");
            }

            Statement stat = con.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            if (rs != null && rs.next()) {
                result = rs.getObject(1);
            }

            con.close();
            return result;
        } catch (Exception var14) {
            logger.error("【getLineResult--指定数据源】执行语句失败：" + sql, var14);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var13) {
                    ;
                }
            }

        }

        return null;
    }

    public static Object[] getLineResult(String sql) {
        printSql(sql);
        Connection con = null;
        Object[] result = null;

        try {
            con = DBPool.getConnection();
            if (con == null) {
                throw new Exception("数据库连接失败");
            }

            Statement stat = con.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            if (rs != null && rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int columnCount = rsmd.getColumnCount();
                result = new Object[columnCount];

                for(int i = 0; i < columnCount; ++i) {
                    result[i] = rs.getObject(i + 1);
                }
            }

            con.close();
        } catch (Exception var16) {
            logger.error("【getLineResult】执行语句失败：" + sql, var16);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var15) {
                    ;
                }
            }

        }

        return result;
    }

    public static Object[] getLineResult(String datasource, String sql) {
        printSql(sql);
        Connection con = null;
        Object[] result = null;

        try {
            con = DBPool.getConnection(datasource);
            if (con == null) {
                throw new Exception("数据库连接失败");
            }

            Statement stat = con.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            if (rs != null && rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int columnCount = rsmd.getColumnCount();
                result = new Object[columnCount];

                for(int i = 0; i < columnCount; ++i) {
                    result[i] = rs.getObject(i + 1);
                }
            }

            con.close();
        } catch (Exception var17) {
            logger.error("【getLineResult】执行语句失败：" + sql, var17);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var16) {
                    ;
                }
            }

        }

        return result;
    }

    public static Object[][] getMultiResult(String sql) {
        printSql(sql);
        Connection con = null;
        Object[][] result = null;

        try {
            con = DBPool.getConnection();
            if (con == null) {
                throw new RuntimeException("数据库连接失败");
            }

            Statement stat = con.createStatement(1004, 1007);
            ResultSet rs = stat.executeQuery(sql);
            if (rs != null && rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int columnCount = rsmd.getColumnCount();
                rs.last();
                int rowCountng = rs.getRow();
                rs.first();
                result = new Object[rowCountng][columnCount];

                for(int i = 0; i < rowCountng; ++i) {
                    for(int j = 0; j < columnCount; ++j) {
                        result[i][j] = rs.getObject(j + 1);
                    }

                    rs.next();
                }
            }

            con.close();
        } catch (Exception var18) {
            logger.error("【getMultiResult】执行语句失败：" + sql, var18);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var17) {
                    ;
                }
            }

        }

        return result;
    }

    public static Object[][] getMultiResult(String datasource, String sql) {
        printSql(sql);
        Connection con = null;
        Object[][] result = null;

        try {
            con = DBPool.getConnection(datasource);
            if (con == null) {
                logger.error("【getMultiResult-指定数据源】数据库连接获取失败");
            }

            Statement stat = con.createStatement(1004, 1007);
            ResultSet rs = stat.executeQuery(sql);
            if (rs != null && rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int columnCount = rsmd.getColumnCount();
                rs.last();
                int rowCountng = rs.getRow();
                rs.first();
                result = new Object[rowCountng][columnCount];

                for(int i = 0; i < rowCountng; ++i) {
                    for(int j = 0; j < columnCount; ++j) {
                        result[i][j] = rs.getObject(j + 1);
                    }

                    rs.next();
                }
            }

            con.close();
        } catch (Exception var19) {
            logger.error("【getMultiResult-指定数据源】执行语句失败：" + sql, var19);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var18) {
                    logger.error("【getMultiResult-指定数据源】数据库执行出现异常", var18);
                }
            }

        }

        return result;
    }

    public static boolean updateBatch(String datasource, List<String> sqlList) {
        Connection con = null;

        try {
            if (datasource == null) {
                con = DBPool.getConnection();
            } else {
                con = DBPool.getConnection(datasource);
            }

            if (con != null) {
                PreparedStatement st = null;

                for(int i = 0; i < sqlList.size(); ++i) {
                    String sql = (String)sqlList.get(i);
                    st = con.prepareStatement(sql);
                    st.executeUpdate();
                    st.close();
                }

                con.commit();
                return true;
            }

            logger.error("【批量更新数据方法】数据库连接获取失败");
        } catch (Exception var17) {
            logger.error("【批量更新数据方法】数据库执行出现异常", var17);

            try {
                con.rollback();
            } catch (SQLException var16) {
                logger.error("【批量更新数据方法】数据库执行出现异常", var16);
                var16.printStackTrace();
            }

            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException var15) {
                    logger.error("【批量更新数据方法】数据库执行出现异常", var15);
                }
            }

        }

        return false;
    }
}
